package com.ruoyi.profession.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.profession.mapper.BasicInfoMapper;
import com.ruoyi.profession.domain.BasicInfo;
import com.ruoyi.profession.service.IBasicInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;

/**
 * 人员基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
@Service
public class BasicInfoServiceImpl extends ServiceImpl<BasicInfoMapper, BasicInfo> implements IBasicInfoService
{
    @Autowired(required = false)
    private BasicInfoMapper basicInfoMapper;

    /**
     * 查询人员基本信息
     * 
     * @param id 人员基本信息主键
     * @return 人员基本信息
     */
    @Override
    public BasicInfo selectBasicInfoById(Long id)
    {
        return basicInfoMapper.selectBasicInfoById(id);
    }

    /**
     * 查询人员基本信息列表
     * 
     * @param basicInfo 人员基本信息
     * @return 人员基本信息
     */
    @Override
    public List<BasicInfo> selectBasicInfoList(BasicInfo basicInfo)
    {
        return basicInfoMapper.selectBasicInfoList(basicInfo);
    }

    /**
     * 新增人员基本信息
     * 
     * @param basicInfo 人员基本信息
     * @return 结果
     */
    @Override
    public int insertBasicInfo(BasicInfo basicInfo)
    {

        BasicInfo basicInfo1 = new BasicInfo();
        //待判断身份证号的唯一性！
        // TODO  待判断身份证号的唯一性！
        boolean idNumber = isIDNumber(basicInfo1.getIdcard());
        return basicInfoMapper.insertBasicInfo(basicInfo);
    }

    /**
     * 修改人员基本信息
     * 
     * @param basicInfo 人员基本信息
     * @return 结果
     */
    @Override
    public int updateBasicInfo(BasicInfo basicInfo)
    {
        return basicInfoMapper.updateBasicInfo(basicInfo);
    }

    /**
     * 批量删除人员基本信息
     * 
     * @param ids 需要删除的人员基本信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicInfoByIds(Long[] ids)
    {
        return basicInfoMapper.deleteBasicInfoByIds(ids);
    }

    /**
     * 删除人员基本信息信息
     * 
     * @param id 人员基本信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicInfoById(Long id)
    {
        return basicInfoMapper.deleteBasicInfoById(id);
    }


    /**
     * 校验身份证号是否合法
     * @param IDNumber
     * @return
     */
    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || (IDNumber.length() != 18 && IDNumber.length() != 15)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = IDNumber.matches(regularExpression);

        //判断第18位校验值
        if (matches) {

            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase() +
                                "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
                        return false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("异常:" + IDNumber);
                    return false;
                }
            }

        }
        return matches;
    }


    //
//    public BasicInfo idIsSole(@RequestBody BasicInfo basicInfo){
//        LambdaQueryWrapper<BasicInfo> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(BasicInfo::getIdcard, 0);//??
//        queryWrapper.eq(BasicInfo::getIdcard, basicInfo.getIdcard().trim());//??
//        if(basicInfo.getId()!=null){
//            queryWrapper.ne(BasicInfo::getId, basicInfo.getId());
//        }
//        long count = IBasicInfoService.count(queryWrapper);
//        if (count > 0) {
//            throw new ServiceException("身份证号错误！已存在！");
//        }
//        return BasicInfo.status(IBasicInfoService.saveOrUpdate(basicInfo));
//    }



//    @PostMapping("/submit")
//    @ApiOperationSupport(order = 6)
//    @ApiOperation(value = "新增或修改", notes = "传入busManage")
//    public R submit(@Valid @RequestBody BusManageEntity busManage) {
//        //车牌号唯一
//        LambdaQueryWrapper<BusManageEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(BusManageEntity::getIsDeleted, 0);
//        queryWrapper.eq(BusManageEntity::getLicensePlate, busManage.getLicensePlate().trim());
//        if(busManage.getId()!=null){
//            queryWrapper.ne(BusManageEntity::getId, busManage.getId());
//        }
//        long count = busManageService.count(queryWrapper);
//        if (count > 0) {
//            throw new ServiceException("车牌号已存在！");
//        }
//        return R.status(busManageService.saveOrUpdate(busManage));
//    }

}
